package com.serie402.web.jsf.mb;

import java.util.List;

import com.kiss.fw.exceptions.BusinessException;
import com.kiss.fw.jsf.mb.AbstractManagedBean;
import com.kiss.fw.utils.Util;
import com.serie402.common.bo.Photo;
import com.serie402.common.bo.PhotoAlbum;
import com.serie402.common.delegate.MultimediaDelegate.GalleryDelegate;
import com.serie402.common.dto.PhotoAlbumDTO;
import com.serie402.common.dto.page.PhotoAlbumPageDTO;
import com.serie402.common.utils.Constants;

public class PhotoAlbumManagedBean extends AbstractManagedBean {
	public String getWindowTitle() {
		return getWindowTitle(Constants.Mappings.PHOTO_ALBUM_DTO);
	}
	
	public String getPageTitle() {
		return getPageTitle(Constants.Mappings.PHOTO_ALBUM_DTO);
	}
	
	public PhotoAlbum getPhotoAlbum() {
		PhotoAlbumPageDTO dto = (PhotoAlbumPageDTO)getRequestScope().get(Constants.Mappings.PHOTO_ALBUM_DTO);
		return dto.getPhotoAlbum();
	}
	
	public String getPhotosViaAjax() {
		int albumId = Util.parseInt((String)getRequestScope().get("albumId"), 0);
		String returnVal = "<select name='photoId'><option value='1'>Option One from Managed Bean</option></select>";
		
		PhotoAlbumDTO dto = new PhotoAlbumDTO();
		dto.setAlbumId(albumId);
		
		try {
			GalleryDelegate.PhotoAlbumDelegate.read(dto);
		}
		catch (BusinessException _exception) {
			_exception.printStackTrace();
		}
		
		if(dto.getAlbums() != null && dto.getAlbums().size() > 0) {
			PhotoAlbum photoAlbum = dto.getAlbums().get(0);
			List<Photo> photos = photoAlbum.getPhotos();
			
			if(photos != null && photos.size() > 0) {
				StringBuffer sb = new StringBuffer("<select name='photoId'>");
				
				for(Photo __photo : photos) {
					sb.append("<option value='");
					sb.append(__photo.getPhotoId());
					sb.append("'>");
					sb.append(__photo.getTitle());
					sb.append("</option>");
				}
				
				sb.append("</select>");
				
				returnVal = sb.toString();
			}
		}
		
		return returnVal;
	}
}